$(function() {
	var innerID;
	var url = ctxPath + "/sys/user/getUser.do";
	var data = {};
	doGetReq(url, data, function(rsp) {
		var retData = rsp.retData;
		innerID = retData.innerID;
	});
	
	$("#normal").keyup(function(){
		$("#final").html("");
	});
	
	$("#test2").bind("click", function() {
		var normal = $("#normal").val();
		var pid = convertStandard(normal);
		if (pid == null) {
			return;
		}
		
		var currentTime = Math.round(new Date().getTime()/1000);
		// 转换既设置cookie
		$.cookie('rakutengj-ORDERINFO', currentTime, {expires: 7});
		var finalSite = convertFinal(currentTime, innerID, pid);
		if (finalSite == null) {
			return;
		}
		$("#final").html("<a href='"+finalSite+"' target='_blank'><h4>点击这里前往乐天国际购买商品或把商品加入购物车</h4></a>");
		$("#alertInfo").html("请买家10分钟内完成购买完并登记！！！");
	});

	$("#clear2").bind("click", function() {
		$("#normal").val("");
		$("#final").html("");
		$("#alertInfo").html("");
	});
});

function convertStandard(source) {
	if ($.trim(source).length === 0 || source.indexOf("global.rakuten.com") == -1) {
		return null;
	}
	if(source.indexOf("?") != -1) {
		source = source.substring(source.indexOf("global.rakuten.com") + 18, source.indexOf("?"));
	} else{
		source = source.substring(source.indexOf("global.rakuten.com") + 18);
	}
	return source;
}

function convertFinal(currentTime, innerID, pid) {
	if ($.trim(innerID).length === 0) {
		return null;
	}
	return "http://www.hth168.com/rakuten.php?uid="+innerID+"&eid="+currentTime+"&pid="+pid;
}